package com.soboot.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.soboot.base.converter.BeanMapping;
import com.soboot.base.model.BaseController;
import com.soboot.base.model.BasePageRes;
import com.soboot.response.Result;
import com.soboot.system.entity.SysDict;
import com.soboot.system.entity.SysMenu;
import com.soboot.system.model.DictSaveReqModel;
import com.soboot.system.model.MenuSaveReqModel;
import com.soboot.system.model.QueryDictListReqModel;
import com.soboot.system.service.ISysDictService;
import com.soboot.system.service.ISysMenuService;
import com.soboot.system.vo.DictVO;
import com.soboot.system.vo.MenuVO;
import io.swagger.annotations.*;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author:
 * @createTime: 2022年12月06日 14:35:48
 * @version:
 * @Description:
 * @Copyright:
 */
@RestController
@RequestMapping("/dicts")
@Api(value="字典管理接口", tags = {"字典管理"})
public class DictController extends BaseController {

    @Autowired
    private ISysDictService dictService;

    @ApiOperation(value = "分页查询", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "reqModel", value = "查询实体", required = true, dataTypeClass = QueryDictListReqModel.class)
    })
    @GetMapping("/page")
    public Result<BasePageRes<DictVO>> page(QueryDictListReqModel reqModel) {

        IPage<SysDict> page = dictService.page(reqModel);
        List<DictVO> list = BeanMapping.mapList(page.getRecords(),DictVO.class);
        BasePageRes<DictVO> result = new BasePageRes<>(page.getCurrent(), page.getSize(), page.getTotal(), list);
        return Result.success(result);
    }

    @ApiOperation(value = "详细信息", notes = "详细信息")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer")
    @GetMapping("/{id}")
    public Result<DictVO> detail(@PathVariable Integer id) {

        DictVO result = dictService.detail(id);
        return Result.success(result);
    }

    @ApiOperation(value = "添加", notes = "添加")
    @ApiImplicitParam(name = "dict", value = "dict", required = true, dataType = "dictDto")
    @PostMapping()
    public Result<String> addDict(@RequestBody @ApiParam @Validated({Insert.class}) DictSaveReqModel reqModel) {

        SysDict sysDict = BeanMapping.map(reqModel,SysDict.class);
        dictService.saveOrUpdate(sysDict);
        return Result.success();
    }

    @ApiOperation(value = "更新", notes = "更新")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "dict", value = "dict", required = true, dataType = "dictDto")
    })
    @PutMapping("/{id}")
    public Result<String> updateDict(@PathVariable Integer id, @RequestBody @ApiParam @Validated({Update.class}) DictSaveReqModel reqModel) {

        SysDict sysDict = BeanMapping.map(reqModel,SysDict.class);
        dictService.saveOrUpdate(sysDict);
        return Result.success();
    }

    @ApiOperation(value = "删除", notes = "删除")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer")
    @DeleteMapping("/{id}")
    public Result<String> delete(@PathVariable Integer id) {

        dictService.delete(id);
        return Result.success();
    }
}
